use blog::Blog;

mod blog;
mod user;
fn main() {
    let user = user::model::new(String::from("hboot"));

    println!("{}", user.get_name());

    let button_primary = Button {
        width: 150,
        height: 150,
        label: String::from("确定"),
    };
    let text_field = TextField {
        width: 200,
        height: 200,
        content: String::from("hello rust, nice!"),
    };

    let screen = Screen {
        components: vec![Box::new(button_primary), Box::new(text_field)],
    };

    screen.run();

    // 面向对象模式
    // let mut blog = Blog::new(String::from("hello rust,"));

    // println!("{:?}", blog.get_content());
    // blog.approval();
    // println!("{:?}", blog.get_content());
    // blog.publish();
    // println!("{:?}", blog.get_content());

    let blog = Blog::new(String::from("hello rust,hboot!"));

    let blog = blog.approval();

    let blog = blog.publish();

    println!("{}", blog.get_content());

    // let name = Option::None;
    // let name = match name {
    //     None => "hboot",
    //     Some(val) => val,
    // };

    // println!("{}", name);

    // let default_name: Option<&str> = Some("admin");

    // if let Some(name) = default_name {
    //     println!("hello! {}", name)
    // };

    // let mut num = 5;
    // while num < 8 {
    //     println!("{}", num);
    //     num += 1;
    // }

    // let arr = [1, 2, 3];
    // for (index, val) in arr.iter().enumerate() {
    //     println!("{}-{}", index, val);
    // }

    // let name = "hboot";
    // match name {
    //     "hboot" => println!("hello"),
    //     "admin" => println!("nice"),
    //     _ => println!("who"),
    // };

    // let name = Some("hboot");
    // let name_other = "test";
    // match name {
    //     Some("admin") => println!("nice"),
    //     Some(name_other) => println!("hello,{}", name_other),
    //     _ => println!("who"),
    // };

    // println!("{}", name_other);

    // let name = "admin";
    // match name {
    //     "hboot" | "admin" => println!("hello,{}", name),
    //     _ => println!("who,{}", name),
    // };

    // let num = 13;
    // match num {
    //     0..=20 => println!("青年"),
    //     21..=50 => println!("壮年"),
    //     _ => println!("老年"),
    // };

    let user = User {
        name: String::from("admin"),
        age: 38,
    };

    // let User {
    //     name: user_name,
    //     age: _,
    // } = user;

    // println!("{}", user_name);

    let _name = "hboot";

    let name = Some(String::from("hboot"));

    if let Some(_) = name {
        println!("不使用匹配值，忽略")
    }
    println!("外部可以继续使用变量，{:?}", name);

    // let User {
    //     name: user_name, ..
    // } = user;

    // println!("{}", user_name);
    // match user {
    //     User { name: "hboot", age } => println!("good,{}", name),
    //     User { name, age: 18 } => println!("青年,{}", name),
    //     User { name, age } => println!("who,{}-{}", name, age),
    // }

    let num = Some(10);
    match num {
        Some(age) if age < 20 => println!("青少年"),
        Some(age) if age < 50 => println!("壮年"),
        _ => println!("{:?}", num),
    }

    match user {
        User {
            age: user_age @ 0..=20,
            ..
        } => println!("{},青少年", user_age),
        User { name, age } => println!("who,{}-{}", name, age),
    }
}

trait Draw {
    fn draw(&self);
}

struct Screen {
    components: Vec<Box<dyn Draw>>,
}

impl Screen {
    fn run(&self) {
        for component in self.components.iter() {
            component.draw();
        }
    }
}

struct Button {
    width: u32,
    height: u32,
    label: String,
}

impl Draw for Button {
    fn draw(&self) {
        // 内容绘制逻辑
        println!("button draw")
    }
}

struct TextField {
    width: u32,
    height: u32,
    content: String,
}

impl Draw for TextField {
    fn draw(&self) {
        // 内容绘制逻辑
        println!("textField draw")
    }
}

struct User {
    name: String,
    age: i32,
}
